package com.wanczy.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.wanczy.pojo.Goods;
import com.wanczy.pojo.User;
import com.wanczy.service.UserService;

@SessionAttributes(value="user")
@Controller
@RequestMapping("/User")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@ResponseBody
	@RequestMapping(value="/showGoods")
	public List<Goods> findByType(@RequestParam("goodsType")String goodsType,Map<String,Object> map){
		List<Goods> list = userService.findByType(goodsType);
		map.put("Goods", list);
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/findGoods")
	public List<Goods> findByNameLike(@RequestParam("shopName")String shopName,@RequestParam("goodsAddress")String goodsAddress,@RequestParam("goodsTaste")String goodsTaste,@RequestParam("goodsName")String goodsName){
		if(shopName.equals("不限")) {
			shopName = null;
		}
		if(goodsAddress.equals("不限")) {
			goodsAddress = null;
		}
		if(goodsTaste.equals("不限")) {
			goodsTaste = null;
		}
		List<Goods> list = userService.findByNameLike(goodsName, shopName, goodsAddress, goodsTaste);
		return list;
	}
	
	@RequestMapping(value="/login")
	public String findUser(@RequestParam("userTel")String userTel,@RequestParam("userPass")String userPass,Map<String,Object> map){
		User user = userService.findUser(userTel, userPass);
		if(null!=user){
			map.put("user", user);
			return  "redirect:/index.jsp";
		}
		return "/login/fail";
	}
	
	
	@RequestMapping(value="/register")
	public String doInsUser(@RequestParam("userTel")String userTel,@RequestParam("userPass")String userPass){
		boolean flag = userService.doInsUser(userTel, userPass);
		if(flag){
			return  "/user/userLogin";
		}
		return "/user/userRegister";
	}
}
